package io.github.smileinmotion.oplog.util;

import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author Mr.spc
 * @date 2022/2/25 18:08
 */
public class MethodParamUtils {

    /**
     * des: 获取方法参数
     *
     * @param args 参数集合
     * @return Object
     */
    public static Object getMethodParam(Object[] args) {
        Object objArg = "";
        for (Object arg : args) {
            if (arg instanceof HttpServletRequest || arg instanceof HttpServletResponse) {
                continue;
            }
            if (arg instanceof BindingResult) {
                objArg = ((BindingResult) arg).getFieldErrors();
            }
            if (arg instanceof MultipartFile) {
                objArg = ((MultipartFile) arg).getOriginalFilename();
            }
            if (arg instanceof MultipartFile[]) {
                MultipartFile[] multipartFiles = (MultipartFile[]) arg;
                objArg = Arrays.stream(multipartFiles).map(MultipartFile::getOriginalFilename).collect(Collectors.joining(","));
            }

        }
        return objArg;
    }
}
